package org.txazo.blog.web.action.json;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.txazo.blog.util.ContextUtil;
import org.txazo.blog.web.action.base.JsonBaseAction;

/**
 * BlogJsonAction
 * 
 * @author Tu XiaoZhou
 * @since 2013-08-28
 * 
 */
@Controller
@Scope("session")
public class BlogJsonAction extends JsonBaseAction {

	private static final long serialVersionUID = 1L;

	private String title;
	private String content;
	private int flag = 0;

	/**
	 * 保存
	 */
	@Action(value = "saveBlog", results = { @Result(name = SUCCESS, type = "json") })
	public String saveBlog() {
		try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(ContextUtil.getBlog1Path() + "/1.html")))) {
			String lineSeparator = "\r\n";
			if (content.contains("\r\n")) {
			} else if (content.contains("\n")) {
				lineSeparator = "\n";
			} else if (content.contains("\r")) {
				lineSeparator = "\r";
			}

			content = content.replace(lineSeparator,
					System.getProperty("line.separator"));

			bw.write(content);
		} catch (IOException e) {
			e.printStackTrace();
		}

		flag = 1;

		return SUCCESS;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}

}
